package cn.iocoder.yudao.module.shop.controller.admin.refundcardrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.refundcardrecord.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.refundcardrecord.ShopRefundCardRecordDO;
import cn.iocoder.yudao.module.shop.convert.refundcardrecord.ShopRefundCardRecordConvert;
import cn.iocoder.yudao.module.shop.service.refundcardrecord.ShopRefundCardRecordService;

@Tag(name = "管理后台 - 退卡记录")
@RestController
@RequestMapping("/shop/refund-card-record")
@Validated
public class ShopRefundCardRecordController {

    @Resource
    private ShopRefundCardRecordService refundCardRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建退卡记录")
    @PreAuthorize("@ss.hasPermission('shop:refund-card-record:create')")
    public CommonResult<Long> createRefundCardRecord(@Valid @RequestBody ShopRefundCardRecordCreateReqVO createReqVO) {
        return success(refundCardRecordService.createRefundCardRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新退卡记录")
    @PreAuthorize("@ss.hasPermission('shop:refund-card-record:update')")
    public CommonResult<Boolean> updateRefundCardRecord(@Valid @RequestBody ShopRefundCardRecordUpdateReqVO updateReqVO) {
        refundCardRecordService.updateRefundCardRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除退卡记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:refund-card-record:delete')")
    public CommonResult<Boolean> deleteRefundCardRecord(@RequestParam("id") Long id) {
        refundCardRecordService.deleteRefundCardRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得退卡记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:refund-card-record:query')")
    public CommonResult<ShopRefundCardRecordRespVO> getRefundCardRecord(@RequestParam("id") Long id) {
        ShopRefundCardRecordDO refundCardRecord = refundCardRecordService.getRefundCardRecord(id);
        return success(ShopRefundCardRecordConvert.INSTANCE.convert(refundCardRecord));
    }

    @GetMapping("/getmember")
    @Operation(summary = "获得会员退卡记录")
    @Parameter(name = "memberId", description = "会员编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:refund-card-record:query')")
    public CommonResult<List<ShopRefundCardRecordDO>> getMemberRefundRecord(@RequestParam("memberId") Long memberId){
        return success(refundCardRecordService.getMemberRefundCardRecord(memberId));
    }

    @GetMapping("/list")
    @Operation(summary = "获得退卡记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:refund-card-record:query')")
    public CommonResult<List<ShopRefundCardRecordRespVO>> getRefundCardRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopRefundCardRecordDO> list = refundCardRecordService.getRefundCardRecordList(ids);
        return success(ShopRefundCardRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得退卡记录分页")
    @PreAuthorize("@ss.hasPermission('shop:refund-card-record:query')")
    public CommonResult<PageResult<ShopRefundCardRecordRespVO>> getRefundCardRecordPage(@Valid ShopRefundCardRecordPageReqVO pageVO) {
        PageResult<ShopRefundCardRecordDO> pageResult = refundCardRecordService.getRefundCardRecordPage(pageVO);
        return success(ShopRefundCardRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出退卡记录 Excel")
    @PreAuthorize("@ss.hasPermission('shop:refund-card-record:export')")
    @OperateLog(type = EXPORT)
    public void exportRefundCardRecordExcel(@Valid ShopRefundCardRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopRefundCardRecordDO> list = refundCardRecordService.getRefundCardRecordList(exportReqVO);
        // 导出 Excel
        List<ShopRefundCardRecordExcelVO> datas = ShopRefundCardRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "退卡记录.xls", "数据", ShopRefundCardRecordExcelVO.class, datas);
    }

}
